/*
 * @Author: Suo-Yuheng syh15399227325@163.com
 * @Date: 2025-09-23 17:15:06
 * @LastEditors: Suo-Yuheng syh15399227325@163.com
 * @LastEditTime: 2025-09-23 20:31:21
 * @FilePath: /0923/Reactor/Main.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include <memory>
#include <functional>
#include "log.hpp"
#include "TcpServer.hpp"    // 处理 IO
#include "Calculator.hpp"   // 处理上层业务

Calculator calculator;


// for debug
void DefaultOnMessage(std::shared_ptr<Connection> connection_ptr)
{
    std::cout << "上层拿到了数据： " << connection_ptr->inbuffer() << std::endl;

    // 处理报文
    std::string inbuffer_copy = connection_ptr->inbuffer();
    std::string response_str = calculator.Handler(inbuffer_copy);  // 因为目前业务逻辑比较简单，不耗时，所以只用这一行即可
    if(response_str.empty())    // 没有完整报文
        return;
    lg(Debug, "%s", response_str.c_str());

    // 发送响应：首先需要把发送数据 放到发送缓冲区里
    connection_ptr->AppendOutbuffer(response_str);
    // 正确地理解发送
    // connection_ptr->_send_cb(connection_ptr);
    connection_ptr->_tcp_server_ptr->Sender(connection_ptr);

}

int main()
{
    

    std::unique_ptr<TcpServer> epoll_svr(new TcpServer(8585, DefaultOnMessage));
    epoll_svr->Init();
    epoll_svr->Loop();


    return 0;
}